/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.cnam.mobileshopping.ws;

import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.ws.rs.Path;
import javax.ws.rs.GET;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Response;
import org.cnam.mobileShopping.entity.MList;
import org.cnam.mobileShopping.entity.Productlist;
import org.cnam.mobileShopping.entity.Store;
import org.cnam.mobileshopping.ejb.list.MListLocal;

/**
 * REST Web Service
 *
 * @author Administrateur
 */
@Stateless
@Path("mobileList")
public class MListeResource {

    @EJB(mappedName = "ejb/mobileshopping/mlist")
    MListLocal listLocal;

    /** Creates a new instance of GenericResource */
    public MListeResource() {
    }

    @GET
    @Path("addList")
    @Produces("application/json")
    public MList addList(@QueryParam("libelle") String libelle, @QueryParam("userId") Integer userId,
    @QueryParam("commonid") String commonid, @QueryParam("statusid") Short statusid, @QueryParam("version") int version) {
        MList liste = listLocal.addList(userId, libelle, commonid, statusid, version);
        if (liste != null) {
            return liste;
        } else {
            return null;
        }
    }
    
    @GET
    @Path("updateList")
    @Produces("application/json")
    public MList updateList(@QueryParam("commonlistId") String commonlistId, @QueryParam("libelle") String libelle, 
                @QueryParam("statusid") Short statusid, @QueryParam("version") int version) {
        MList liste = listLocal.updateList(commonlistId, libelle, statusid, version);
        if (liste != null) {
            return liste;
        } else {
            return null;
        }
    }
    
    @GET
    @Path("deleteList")
    @Produces("application/json")
    public void deleteList(@QueryParam("commonlistId") String commonlistId, @QueryParam("userId") Integer userId) {
        listLocal.deleteList(commonlistId, userId);
    }

    @GET
    @Path("getList")
    @Produces("application/json")
    public Response get(@QueryParam("commonlistid") String commonlistId) {
        MList liste = listLocal.getListByCommonId(commonlistId);
        if (liste != null) {
            return Response.ok(liste).build();
        } else {
            return Response.status(Response.Status.SERVICE_UNAVAILABLE).build();
        }
    }

    @GET
    @Path("addProduct")
    @Produces("application/json")
    public Response addProductInList(@QueryParam("commonlistId") String commonlisteId, 
    @QueryParam("userId") Integer userId, @QueryParam("produitId") Integer productId, 
    @QueryParam("quantity") Integer quantity, @QueryParam("commonid") String commonid, @QueryParam("version") int version) {
        Productlist productslist = listLocal.addProductInList(commonlisteId, productId, userId, quantity, commonid, version);
        if (productslist != null) {
            return Response.ok(productslist).build();
        } else {
            return Response.status(Response.Status.SERVICE_UNAVAILABLE).build();
        }
    }
    
    @GET
    @Path("updateProduct")
    @Produces("application/json")
    public Response updateProductInList(@QueryParam("commonproductListId") String commonProductListeId, 
                                        @QueryParam("userId") Integer userId, 
                                        @QueryParam("quantity") Integer quantity, 
                                        @QueryParam("version") int version, 
                                        @QueryParam("status") int status) {
        Productlist productslist = listLocal.updateProductInList(commonProductListeId, userId, quantity, status, version);
        if (productslist != null) {
            return Response.ok(productslist).build();
        } else {
            return Response.status(Response.Status.SERVICE_UNAVAILABLE).build();
        }
    }
    
    @GET
    @Path("deleteProduct")
    @Produces("application/json")
    public Response deleteProductInList(@QueryParam("commonproductListId") String commonProductListeId, @QueryParam("userId") Integer userId) {
         listLocal.deleteProductInList(commonProductListeId, userId);
         return Response.status(Response.Status.OK).build();
    }

    @GET
    @Path("getProductsListByComonId")
    @Produces("application/json")
    public List<Productlist> getProductsListByComonId(@QueryParam("commonlistid") String commonlisteId) {
        List<Productlist> products = listLocal.getProductsListByCommonId(commonlisteId);
        if(products != null){
            return products;
        }
        else {
            return null;
        }
    }
    
    @GET
    @Path("getListsForUser")
    @Produces("application/json")
    public List<MList> getListsForUser(@QueryParam("userId") Integer userId) {
        return listLocal.getListsForUser(userId);
    }
    
    
    @GET
    @Path("sharedList")
    @Produces("application/json")
    public void sharedList(@QueryParam("userId") Integer userId, @QueryParam("commonlistid") String commonlistid) {
        listLocal.sharedist(userId, commonlistid);
    }
    
    @GET
    @Path("getItinerary")
    @Produces("application/json")
    public List<Store> getStores(@QueryParam("commonlistid") String commonlistid) {
        List<Store> stores = new ArrayList<Store>();
        
        stores.add(new Store(1,"Casino La cépière", 1.415151, 43.58997));
        stores.add(new Store(2,"Auchan Balma", 1.490936, 43.662507));
        stores.add(new Store(3,"E.LECLERC Blagnac", 1.370487, 43.653044));
        return stores;
    }
}
